package demo1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) {
        String host = "127.0.0.1";
        int port = 5555;

        try (Socket socket = new Socket(host, port);
             PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
             BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
             Scanner scanner = new Scanner(System.in)) {

            System.out.println("已连接服务器 " + host + ":" + port);
            System.out.println("请输入要发送的字符串（输入 -1 退出）：");

            while (true) {
                String message = scanner.nextLine();
                out.println(message);

                if ("-1".equals(message.trim())) {
                    String byeMsg = in.readLine();
                    System.out.println("服务器返回: " + byeMsg);
                    break;
                }

                String response = in.readLine();
                System.out.println("服务器返回: " + response);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
